#pragma once

#include "EventLoop.h"
#include <vector>
#include <map>

struct epoll_event;
class Channel;
class TcpConnection;

class EPoller {
public:
    typedef std::vector<Channel*> ChannelList;

    EPoller(EventLoop* loop);
    ~EPoller();

    int poll( int timeoutMs, ChannelList* activeChannels);

    void updateChannel(Channel* channel);
    void removeChannel(Channel* channel);
    
private:
    static const int kInitEventListSize = 16;

    
    void update(int operation, Channel* channel);   // 更新Channel
    void fillActiveChannels(int numEvents, ChannelList* activeChannels) const;
    
    typedef std::vector<struct epoll_event> EventList;
    typedef std::map<int, Channel*> ChannelMap;
    
    EventLoop* ownerLoop_;
    int epollfd_;       // epoll_creat()创建
    EventList events_;  // epoll_wait返回的活动fd列表，大小自适应
    ChannelMap channels_;

};